package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.UserMapper;
import com.situ.springboot.pojo.User;
import com.situ.springboot.pojo.query.UserQuery;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> selectByPage(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPage(), userQuery.getLimit());
        List<User> list = userMapper.selectByPage(userQuery);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        return new PageResult<>(0, "", totalCount, list);
    }

    @Override
    public User login(String name, String password) {
        return userMapper.login(name,password);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            userMapper.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public User selectById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }
}
